module.exports   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
1 1
var jwt = require('jsonwebtoken');
2
3 1
module.exports = function(secret) {
4 1
    return function(req, res, next) {
5
        let token;
6 3
        if (req.headers.authorization) {
7
            token = req.headers['authorization'].split(' ')[1];
8
        } else {
9 3
            token = req.body.token || req.query.token || req.headers['x-access-token'];
10
        }
11 3
        if (token) {
12 2
            jwt.verify(token, secret, function(err, decoded) {
13 2
                if (err) {
14 1
                    return res.status(403).send({ success: false, message: 'Failed to authenticate token.' });
15
                } else {
16 1
                    req.decoded = decoded;
17 1
                    next();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
18
                }
19
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
20
        } else {
21 1
            return res.status(403).send({
22
                success: false,
23
                message: 'No token provided.'
24
            });
25
        }
26
    }
27
}
28